package com.example.csmallpassport.controller;

import com.alibaba.fastjson.JSON;
import com.example.csmallpassport.pojo.dto.AdminAddNewDTO;
import com.example.csmallpassport.pojo.dto.AdminLoginDTO;
import com.example.csmallpassport.pojo.dto.AdminUpdateDTO;
import com.example.csmallpassport.pojo.entity.Admin;
import com.example.csmallpassport.pojo.vo.AdminListItemVO;
import com.example.csmallpassport.pojo.vo.AdminStandardVO;
import com.example.csmallpassport.security.AdminDetails;
import com.example.csmallpassport.security.LoginPrincipal;
import com.example.csmallpassport.service.AdminService;
import com.example.csmallpassport.web.JsonResult;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.xml.ws.Service;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    AdminService adminService;

    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO) {
        log.debug("开始处理【管理员登录】的请求，参数:{}", adminLoginDTO);
        //TODO 具体功能待定
        String login = adminService.login(adminLoginDTO);
        return JsonResult.ok(login);
    }

    @PostMapping("/add-new")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO) {
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        adminService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> Enable(@PathVariable Long id) {
        adminService.enable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> Disable(@PathVariable Long id) {
        log.debug("id:{}",id);
        adminService.disable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult update(@PathVariable Long id, AdminUpdateDTO adminUpdateDTO) {
        log.debug("id：{}",id);
        adminService.update(id,adminUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    public JsonResult<String> getStandardById(@PathVariable Long id) {
        AdminStandardVO standardById = adminService.getStandardById(id);
        //利用JSON框架将数据转换为JSON类型的字符串
        String s = JSON.toJSONString(standardById);
        return JsonResult.ok(s);
    }

    @GetMapping("")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<List<AdminListItemVO>> list() {
        //@AuthenticationPrincipal @ApiIgnore LoginPrincipal loginPrincipal
        //@AuthenticationPrincipal 将SecurityContext中的Principal对象中信息存添加到当前作为参数的User对象当中
        //并且，此注解只有是再Spring相关框架调用的方法
        //log.warn("当事人ID:{}", loginPrincipal.getId());
        //log.warn("当事人用户名:{}", loginPrincipal.getUsername());
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }

}
